/**
 * Created On: Sep 9, 2012
 */
package com.athena.dal.dao;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.athena.dal.entities.User;

/**
 * @author Ashish Kumar Sinha <aksinha.mail@gmail.com>
 * Version 1.0
 */
public class UserDaoImpl implements UserDao {
	
	@Autowired
	private GenericDao genericDao;

	/**
	 * @return the genericDao
	 */
	public GenericDao getGenericDao() {
		return genericDao;
	}

	/**
	 * @param genericDao the genericDao to set
	 */
	public void setGenericDao(GenericDao genericDao) {
		this.genericDao = genericDao;
	}

	/* (non-Javadoc)
	 * @see com.athena.dal.dao.UserDao#findByUsername(java.lang.String)
	 */
	@Override
	@Transactional(readOnly = true)
	public User findByUsername(String username) {
		User user = null;
		if(username != null && genericDao != null){
			EntityManager entityManager = genericDao.getEntityManager();
			if(entityManager != null){
				user = (User) entityManager.createNamedQuery("User.findByUsername").setParameter("username", username).getSingleResult();
			}else {
				System.err.println(getClass().getName() + " :: Entity Manager is NULL");
			}
		}
		return user;
	}

}
